<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Session;
class User  extends Common
{
    
    

//以下是管理员

    public function master()
    {
        $admin_level=Db::name('admin_level')->order('id')->select();
        $this->view->assign('admin_level', $admin_level);
        return $this->view->fetch();
    }
    

    public function masterall()
    {
        //数量
        $leaveCount = Db::name("admin")->count();

        //分页数据
        $limit=input('limit');
        $page=input('page');
        $start=($page-1)*$limit;

        //调取数据
        $userall=Db::name("admin")->limit($start,$limit)->order('id')->select();

        foreach ($userall as $key => $v) {
            if($v['ini']==1){
                $userall[$key]['type']='超级管理员';
            }else{
                $userall[$key]['type']='管理员';
            }
           $userall[$key]['ID']=$key+1;
        }
        //API数据格式处理
        $data= array();
        $data['code']=0;
        $data['msg']='';
        $data['count']=$leaveCount;
        $data['data']=$userall;

        //返回json数据
        echo $data=json_encode($data);
    }


    public function addmaster(){

         $data=input('data');
         $data=json_decode($data,true);
         $where['master']=$data['master'];
         $check=Db::name("admin")->where($where)->find();

         if($check){
            return '99';
         }

         $data['pword']=md5($data['pword']);
         $data['addtime']=date('Y-m-d H:i:s',time());

            $user=SESSION::get('cmuser');
            $log_title=$where['master'];
            $log="add user ' $log_title '";
            set_log($user,$log);


        if(Db('admin') -> insert($data)){
            return '1';
        }else{
            return '0';
        }
       
    }


    public function delmasters(){
       
            $json =input('num');
            $del_datas = json_decode($json, true);

            
            
            $del=array();

            $admin=Db::name('admin');
            
            foreach ($del_datas as $key => $value) {
                $del['id']=$value['id'];
                $admin->where($del)->delete();
                $log_id=$log_id.'|'.$v['id'];
            }

            $user=SESSION::get('cmuser');
            $log_title=$log_id;
            $log="delete user ' $log_title '";
            set_log($user,$log);


            return '1';
         
        
    }

    public function editmaster(){
          $json=input('data');
          $del_datas = json_decode($json, true);
          $where['id']=$del_datas['id'];
          if($del_datas['pword']){
            $datas['pword']=md5($del_datas['pword']);
          }
          if($del_datas['ini']){
            $datas['ini']=$del_datas['ini'];
          }


          $user=SESSION::get('cmuser');
            $log_title=$where['id'];
            $log="edit user ' $log_title '";
            set_log($user,$log);

          $admin=Db::name('admin');
          $info=$admin->where($where)->update($datas);
          
          if($info){
            return '1';
          }else{
            return '2';
          }

         
    }

    public function delmaster(){
        $del['id']=input('id');

        $user=SESSION::get('cmuser');
            $log_title=$ldel['id'];
            $log="delete user ' $log_title '";
            set_log($user,$log);

        if(Db::name("admin")->where($del)->delete()){
            return '1';
        }else{
            return '99';
        }
    }

//以下是会员

    public function user()
    {
        
        return $this->view->fetch();
    }
    public function userall()
    {
                //数量
        $leaveCount = Db::name("user")->count();

        //分页数据
        $limit=input('limit');
        $page=input('page');
        $start=($page-1)*$limit;

        //调取数据
        $userall=Db::name("user")->limit($start,$limit)->order('id')->select();

        foreach ($userall as $k => $v) {
            $userall[$k]['wxname']=base64_decode($v['username']);
            if($v['telphone']){
                $userall[$k]['integral']=tel_get_integral($v['telphone']);
            }
            
        }

        
        //API数据格式处理
        $data= array();
        $data['code']=0;
        $data['msg']='';
        $data['count']=$leaveCount;
        $data['data']=$userall;

        //返回json数据
        echo $data=json_encode($data);
    }
    public function active()
    {
        $info['id']=input('id');
        $info['type']=input('type');
        $this->view->assign('info', $info);
        return $this->view->fetch();
    }
    public function active_save()
    {
        $data=input('data');
        $data=json_decode($data,true);
        $user=get_user($data['id']);
        $active=erp_active($user['telphone'],$data['type'],$data['active'],$user['openid']);
        $active=json_decode($active,true);
        if($active['code']!=10000){
            return 2;
        }
        $data_l['userid']=$data['id'];
        $data_l['integral']=$data['active'];
        $data_l['type']=$data['type'];
        $data_l['remain']=$active['integral'];
        $data_l['remake']=$data['remake'];
        $data_l['time']=date('Y-m-d H:i:s',time());
        Db::name('integral_log') -> insert($data_l);


        if($data['type']==1){
            $bd='增加'.$data['active'].'积分';
        }else if($data['type']==2){
            $bd='减少'.$data['active'].'积分';
        }else{
            $bd='';
        }
        

        //微信积分提醒
        $data = array(
            'first' => array(
                'value' => '尊敬的会员您积分变动如下:',
                'color' => '#FF0000'
            ),
            'keyword1' => array(
                'value' => $bd,
                'color' => '#000'
            ),
            'keyword2' => array(
                'value' => '已完成',
                'color' => '#000'
            ),
            'keyword3' => array(
                'value' => $data_l['remain'],
                'color' => '#000'
            ),
            'remark' => array(
                'value' => $data_l['remake'].'，'.'感谢您再次光临！',
                'color' => '#000'
            )
        );
        $wxurl='http://www.sxjhol.vip/index.php/index/user';
        send_wx($data,$user['openid'],'61KQO8sAOYJLqZM8bwGsIFxnmE608MEv0ueie9iG9yA',$wxurl);

            $userlog=SESSION::get('cmuser');
            $log_title=$bd;
            $log="edit user integral ' $log_title '";
            set_log($userlog,$log);



        return 1;
    }
    public function power(){
        return $this->view->fetch();
    }
    public function set_power(){
        $id=input('id');
        $info=Db::name('admin')->find($id);
        $info['power']=explode('|', $info['power']);
        $this->view->assign('info', $info);
        $this->view->assign('Assets', Assets);
        return $this->view->fetch();
    }
    public function set_power_save(){
        $data=input('data');
        $datas=json_decode($data,true);

        for ($i=0; $i < 30 ; $i++) { 
            if($i !=0){
                $power=$power.'|';
            }
            if($datas[$i]){
                $power=$power.'1';
            }
            
        }
        $save['power']=$power;

            $userlog=SESSION::get('cmuser');
            $log_title=$datas['id'];
            $log="edit user power ' $log_title '";
            set_log($userlog,$log);

        $info=Db::name('admin')->where('id',$datas['id'])->update($save);
        if($info){
            return '1';
        }else{
            return '99';
        }
    }
    
    public function sync(){
        $userDb=Db::name('user');
        $user_integral=Db::name('user_integral');
        $user=$userDb->select();
        foreach ($user as $k => $v) {
            if($v['telphone']){
                $info=erp_active($v['telphone']);
                $info=json_decode($info,true);
                $check=$user_integral->where('tel',$v['telphone'])->find();
                if($check){
                    $date['integral']=$info['integral'];
                    $user_integral->where('tel',$v['telphone'])->update($date);
                }else{
                    $user_integral->insert(array('tel' =>$v['telphone'],'integral'=>$info['integral']));
                }
            }
        }
        $userlog=SESSION::get('cmuser');
            
            $log="sync user integral";
            set_log($userlog,$log);

        return 1;
    }
 
 

}
